package cn.dlc.com;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author dlc
 * @Date 2023/11/24
 * @Description TODO
 * @Version 0.0.1
 */
@SpringBootApplication
public class CoreApplication {

    public static void main(String[] args) {
        SpringApplication.run(CoreApplication.class, args);
    }

    @Autowired
    private RedisTemplate redisTemplate;


    public void test(){
        // 对于mset
        // redisTemplate.opsForValue().multiSet();


        List list = redisTemplate.executePipelined(
                new RedisCallback<Object>() {
                    @Override
                    public Object doInRedis(RedisConnection connection) throws DataAccessException {
                        // 1、通过connection打开pipeline
                        connection.openPipeline();
                        // 2、给本次pipeline添加一次性要执行的多条命令

                        // 2.1、一个 set key value 的操作
                        byte[] key = "name".getBytes();
                        byte[] value = "qinyi".getBytes();
                        connection.set(key, value);

                        // 2.2、执行一个错误的命令
                        connection.lPop("xyzabc".getBytes());

                        // 2.3、mset 操作
                        Map<byte[], byte[]> tuple = new HashMap<>();
                        tuple.put("id".getBytes(), "1".getBytes());
                        tuple.put("age".getBytes(), "19".getBytes());
                        connection.mSet(tuple);

                        /**
                         * 1、不能关闭pipeline
                         * 2、返回值为null
                         */
                        // 3. 关闭 pipeline
                        // connection.closePipeline();
                        return null;
                    }
                }
        );

        // 输出
        list.forEach(System.out::println);

    }
}
